/**
 * 博客相关路由 - Express 路由
 */
import express from 'express'
import BlogHandler from '../handlers/blog.js'
import { validateListParams, validateSearchListParams } from '../middleware/index.js'

const router = express.Router()

// 健康检查
router.get('/health', (req, res) => {
  res.json({
    code: 200,
    message: 'success',
    data: {
      status: 'ok',
      timestamp: Date.now(),
      environment: 'mock-server',
      version: '1.0.0',
      uptime: process.uptime(),
    },
  })
})

// 文章相关路由（注意顺序：具体路由在前，通用路由在后）
router.get('/articles/search', validateSearchListParams, (req, res) => {
  // 使用验证后的参数
  const query = {
    ...req.query,
    page: req.pagination.page,
    limit: req.pagination.limit,
    sort: req.sort,
    ...req.filters,
  }

  const response = BlogHandler.searchArticles(query)
  res.status(response.code).json(response)
})

router.get('/articles', validateListParams, (req, res) => {
  // 使用验证后的参数
  const query = {
    ...req.query,
    page: req.pagination.page,
    limit: req.pagination.limit,
    sort: req.sort,
    ...req.filters,
  }

  const response = BlogHandler.getArticles(query)
  res.status(response.code).json(response)
})

router.get('/article/:id', (req, res) => {
  // 判断参数是数字ID还是字符串slug
  const param = req.params.id
  let response

  if (/^\d+$/.test(param)) {
    // 纯数字，按ID查询
    response = BlogHandler.getArticleById(param)
  } else {
    // 包含字符，按slug查询
    response = BlogHandler.getArticleBySlug(param)
  }

  res.status(response.code).json(response)
})

router.post('/articles', (req, res) => {
  const response = BlogHandler.createArticle(req.body)
  res.status(response.code).json(response)
})

router.put('/articles/:id', (req, res) => {
  const response = BlogHandler.updateArticle(req.params.id, req.body)
  res.status(response.code).json(response)
})

router.delete('/articles/:id', (req, res) => {
  const response = BlogHandler.deleteArticle(req.params.id)
  res.status(response.code).json(response)
})

// 批量操作
router.post('/articles/batch-update', (req, res) => {
  const { articleIds, updateData } = req.body
  const response = BlogHandler.batchUpdateArticles(articleIds, updateData)
  res.status(response.code).json(response)
})

// 文章审核
router.post('/articles/:id/review', (req, res) => {
  const response = BlogHandler.updateArticle(req.params.id, {
    status: req.body.status === 'approved' ? 'published' : 'archived',
    reviewComment: req.body.comment,
    reviewDate: new Date().toISOString(),
  })
  res.status(response.code).json(response)
})

// 协作编辑
router.post('/articles/:id/collaborate', (req, res) => {
  res.json({
    code: 200,
    message: 'success',
    data: {
      sessionId: `session_${req.params.id}_${Date.now()}`,
      websocketUrl: `ws://localhost:3001/ws/collaborate/${req.params.id}`,
    },
  })
})

// 分类相关路由
router.get('/categories', (req, res) => {
  const response = BlogHandler.getCategories()
  res.status(response.code).json(response)
})

// 标签相关路由
router.get('/tags', (req, res) => {
  const response = BlogHandler.getTags()
  res.status(response.code).json(response)
})

// 归档路由
router.get('/archive', (req, res) => {
  const response = BlogHandler.getArchive()
  res.status(response.code).json(response)
})

// 统计路由
router.get('/stats', (req, res) => {
  const response = BlogHandler.getStats()
  res.status(response.code).json(response)
})

export default router
